//
//  SendMessageViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/7/21.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "SendMessageViewController.h"
#import "IQKeyboardManager.h"
#import "CustomMessageCell.h"
#import "Config.h"
#import "OneOrderViewController.h"
#import "InfoApi.h"
#import "ConsultantInformation.h"

@interface SendMessageViewController ()<EaseMessageViewControllerDelegate>
{
    NSString *_loginUser;
    NSString *_ortherUser;
}
@end

@implementation SendMessageViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.showRefreshHeader = YES;
    self.dataSource = self;
    self.delegate = self;
    self.title = _userInfo.nickname;
    
    [self.chatBarMoreView removeItematIndex:4];
    [self.chatBarMoreView removeItematIndex:3];
    [self.chatBarMoreView removeItematIndex:1];

//    self.navigationItem.rightBarButtonItem = [Tool BarButtonItemWithTitle:@"发送" target:self action:@selector(send)];
    
}

-(void)send
{
    UIAlertController *vc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    
    for (int i = 0; i < 15; i++) {
        [vc addAction:[UIAlertAction actionWithTitle:[Tool TextWithType:i] style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [self sendTextMessage:[Tool TextWithType:i] withExt:@{CustomMessageTypeKey:[Tool ExtCustomMessageTypeKey:i],
                                                       CustomMessageOrderIDKey:@(100),
                                                       CustomMessageServiceIDKey:@(200),
                                                       CustomMessageScaleIDKey:@(300),
                                                       CustomMessageScaleTitleKey:@"测试量表"}];
        }]];
    }
    [self presentViewController:vc animated:YES completion:nil];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id model = [self.dataArray objectAtIndex:indexPath.row];
    EaseMessageModel *messageModel = model;

    if (![model isKindOfClass:[NSString class]]) {
    
        EaseBaseMessageCell *cell = (EaseBaseMessageCell *)[super tableView:tableView cellForRowAtIndexPath:indexPath];
        [cell.bubbleView.textLabel.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        if ([messageModel.message.ext objectForKey:CustomMessageTypeKey]) {
            cell.messageTextColor = [@"65ACFE" colorWithHexString];
            
            UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
            [btn addTarget:self action:@selector(pushOrder:) forControlEvents:(UIControlEventTouchUpInside)];
            [cell.bubbleView.textLabel addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(cell.bubbleView.textLabel);
            }];
            
            cell.bubbleView.textLabel.userInteractionEnabled = YES;
            cell.bubbleView.backgroundImageView.userInteractionEnabled = YES;
        }else{
            cell.messageTextColor = [UIColor blackColor];
            cell.bubbleView.textLabel.userInteractionEnabled = NO;
            cell.bubbleView.backgroundImageView.userInteractionEnabled = NO;
        }
        return cell;
    }else{
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }
}



- (id<IMessageModel>)messageViewController:(EaseMessageViewController *)viewController
                           modelForMessage:(EMMessage *)message{
    id<IMessageModel> model = nil;
    model = [[EaseMessageModel alloc] initWithMessage:message];
    model.avatarImage = [UIImage imageNamed:@"EaseUIResource.bundle/user"];
    if (message.direction == EMMessageDirectionSend) {
        //自己
        model.avatarURLPath = [ConsultantInformation sharedInformation].avatar;
        model.nickname = [ConsultantInformation sharedInformation].name;
    }else{
        model.avatarURLPath = _userInfo.avatar;
        model.nickname = _userInfo.nickname;
    }
    
    model.failImageName = @"imageDownloadFail";
    return model;
}

-(void)pushOrder:(UIButton *)btn
{
    CGPoint point = [btn convertPoint:btn.frame.origin toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
    
    id model = [self.dataArray objectAtIndex:indexPath.row];
    EaseMessageModel *messageModel = model;
    
    OneOrderViewController *vc = [[OneOrderViewController alloc]init];
    vc.orderID = [[messageModel.message.ext objectForKey:CustomMessageOrderIDKey] intValue];
    [self.navigationController pushViewController:vc animated:YES];
}


//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    id model = [self.dataArray objectAtIndex:indexPath.row];
//    
//    if (![model isKindOfClass:[NSString class]]) {
//        EaseMessageModel *messageModel = model;
//        if (messageModel.bodyType == EMMessageBodyTypeText && messageModel.message.ext) {
//            return [CustomMessageCell cellHeightWithModel:model];
//        }else{
//            return [super tableView:tableView heightForRowAtIndexPath:indexPath];
//        }
//    }else{
//        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
//    }
//}

#pragma mark - EaseMessageViewControllerDelegate
- (BOOL)messageViewController:(EaseMessageViewController *)viewController
   canLongPressRowAtIndexPath:(NSIndexPath *)indexPath
{
    id model = [self.dataArray objectAtIndex:indexPath.row];
    EaseMessageModel *messageModel = model;
    if ([messageModel.message.ext objectForKey:CustomMessageTypeKey]) {
        return NO;
    }
    return YES;
}

- (BOOL)messageViewController:(EaseMessageViewController *)viewController
   didLongPressRowAtIndexPath:(NSIndexPath *)indexPath
{
    id object = [self.dataArray objectAtIndex:indexPath.row];
    if (![object isKindOfClass:[NSString class]]) {
        EaseMessageCell *cell = (EaseMessageCell *)[self.tableView cellForRowAtIndexPath:indexPath];
        [cell becomeFirstResponder];
        self.menuIndexPath = indexPath;
        [self showMenuViewController:cell.bubbleView andIndexPath:indexPath messageType:cell.model.bodyType];
    }
    return YES;
}



 


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [self.tableView reloadData];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    (EaseChatToolbar*)self.chatToolbar.inputTextView
    EaseChatToolbar *toolbar = (EaseChatToolbar*)self.chatToolbar;
    [toolbar.inputTextView resignFirstResponder];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
}



-(void)dealloc
{
    NSLog(@"SendMessageViewController 释放");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
